<template>
  <div class="login-page">
    <div class="login-container">
      <!-- 标题 -->
      <h2>登录</h2>

      <!-- 登录表单 -->
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
              type="text"
              id="username"
              v-model="loginForm.username"
              placeholder="请输入用户名"
              required
          />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input
              type="password"
              id="password"
              v-model="loginForm.password"
              placeholder="请输入密码"
              required
          />
        </div>
        <div class="form-options">
          <label>
            <input type="checkbox" v-model="loginForm.rememberMe" /> 记住我
          </label>
          <a href="/forgetPage">忘记密码？</a>
        </div>
        <button type="submit">登录</button>
      </form>

      <!-- 社交登录 -->
      <div class="social-login">
        <p>或使用以下方式登录</p>
        <div class="social-buttons">
          <button class="social-btn wechat" @click="loginWithWechat">
            <i class="fab fa-weixin"></i> 微信
          </button>
          <button class="social-btn weibo" @click="loginWithWeibo">
            <i class="fab fa-weibo"></i> 微博
          </button>
        </div>
      </div>

      <!-- 注册链接 -->
      <div class="register-link">
        <p>还没有账号？<a href="/register">立即注册</a></p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import {login,register} from '@/api/login.js'
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router'; // 引入 vue-router
import { useStore } from 'vuex'; // 引入 Vuex store
// 登录表单数据
const loginForm = ref({
  username: '老板',
  password: '123456',
  rememberMe: false,
});
const router = useRouter();
const store = useStore();
// 处理登录
const handleLogin = () => {
  const { username, password, rememberMe } = loginForm.value;
  if (username && password) {
    console.log('登录信息:', { username, password, rememberMe });

     login(loginForm.value)
     .then((res)=>{
        console.log(res);
       if(res.state===0){
        ElMessage.success(res.message || '登录成功！');
       store.commit('SET_USER', res.data); // 假设返回的数据包含 userInfo

        router.push('/homepage');
       }

     })
  } else {
    alert('请输入用户名和密码！');
  }
};

// 微信登录
const loginWithWechat = () => {
  alert('微信登录');
  // 这里可以调用微信登录 API
};

// 微博登录
const loginWithWeibo = () => {
  alert('微博登录');
  // 这里可以调用微博登录 API
};
</script>

<style scoped>
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700&display=swap');
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css');

.login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #0e9aa7, #057885);
  font-family: 'Noto Sans SC', sans-serif;
  padding: 20px;
}

.login-container {
  background-color: white;
  border-radius: 15px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 400px;
  text-align: center;
}

h2 {
  font-size: 1.8rem;
  margin-bottom: 20px;
  color: #333;
}

.form-group {
  margin-bottom: 20px;
  text-align: left;
}

label {
  display: block;
  font-size: 0.9rem;
  color: #555;
  margin-bottom: 5px;
}

input[type='text'],
input[type='password'] {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 8px;
  font-size: 1rem;
  outline: none;
  transition: border-color 0.3s ease;
}

input[type='text']:focus,
input[type='password']:focus {
  border-color: #0e9aa7;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  font-size: 0.9rem;
}

.form-options label {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #555;
}

.form-options a {
  color: #0e9aa7;
  text-decoration: none;
  transition: color 0.3s ease;
}

.form-options a:hover {
  color: #057885;
}

button[type='submit'] {
  width: 100%;
  padding: 12px;
  background-color: #0e9aa7;
  border: none;
  border-radius: 8px;
  color: white;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

button[type='submit']:hover {
  background-color: #057885;
}

.social-login {
  margin-top: 30px;
}

.social-login p {
  font-size: 0.9rem;
  color: #666;
  margin-bottom: 15px;
}

.social-buttons {
  display: flex;
  justify-content: center;
  gap: 15px;
}

.social-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.social-btn.wechat {
  background-color: #1aad19;
  color: white;
}

.social-btn.wechat:hover {
  background-color: #169116;
}

.social-btn.weibo {
  background-color: #e6162d;
  color: white;
}

.social-btn.weibo:hover {
  background-color: #c41426;
}

.register-link {
  margin-top: 20px;
  font-size: 0.9rem;
  color: #666;
}

.register-link a {
  color: #0e9aa7;
  text-decoration: none;
  transition: color 0.3s ease;
}

.register-link a:hover {
  color: #057885;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-container {
    padding: 20px;
  }

  h2 {
    font-size: 1.5rem;
  }

  .social-buttons {
    flex-direction: column;
    gap: 10px;
  }

  .social-btn {
    width: 100%;
    justify-content: center;
  }
}
</style>